package net.minecraft.block;

import com.mojang.serialization.MapCodec;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.state.StateManager;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;

public class EndRodBlock extends RodBlock {
	public static final MapCodec<EndRodBlock> CODEC = createCodec(EndRodBlock::new);

	@Override
	public MapCodec<EndRodBlock> getCodec() {
		return CODEC;
	}

	protected EndRodBlock(AbstractBlock.Settings settings) {
		super(settings);
		this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.UP));
	}

	@Override
	public BlockState getPlacementState(ItemPlacementContext ctx) {
		Direction direction = ctx.getSide();
		BlockState blockState = ctx.getWorld().getBlockState(ctx.getBlockPos().offset(direction.getOpposite()));
		return blockState.isOf(this) && blockState.get(FACING) == direction
			? this.getDefaultState().with(FACING, direction.getOpposite())
			: this.getDefaultState().with(FACING, direction);
	}

	@Override
	public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
		Direction direction = state.get(FACING);
		double d = pos.getX() + 0.55 - random.nextFloat() * 0.1F;
		double e = pos.getY() + 0.55 - random.nextFloat() * 0.1F;
		double f = pos.getZ() + 0.55 - random.nextFloat() * 0.1F;
		double g = 0.4F - (random.nextFloat() + random.nextFloat()) * 0.4F;
		if (random.nextInt(5) == 0) {
			world.addParticleClient(
				ParticleTypes.END_ROD,
				d + direction.getOffsetX() * g,
				e + direction.getOffsetY() * g,
				f + direction.getOffsetZ() * g,
				random.nextGaussian() * 0.005,
				random.nextGaussian() * 0.005,
				random.nextGaussian() * 0.005
			);
		}
	}

	@Override
	protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
		builder.add(FACING);
	}
}
